package acwing;

import java.io.*;
import java.util.*;

public class ac858 {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
    static final int N = 510, INF = 0x3f3f3f3f;
    static int n, m;
    static int[][] g = new int[N][N];
    static int[] d = new int[N];
    static boolean[] st = new boolean[N];

    static int cin() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    static int prim() {
        Arrays.fill(d, INF);
        int res = 0;
        for (int i = 0; i < n; i ++ ) {
            int t = -1;
            for (int j = 1; j <= n; j ++ ) {
                if (!st[j] && (t == -1 || d[t] > d[j])) {
                    t = j;
                }
            }
            if (i != 0 && d[t] == INF) return INF;
            if (i != 0) res += d[t];
            st[t] = true;
            for (int j = 1; j <= n; j ++ ) d[j] = Math.min(d[j], g[t][j]);
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        n = cin(); m = cin();
        for (int i = 0; i < N; i ++ ) Arrays.fill(g[i], INF);
        while (m -- > 0) {
            int a = cin(), b = cin(), c = cin();
            g[a][b] = g[b][a] = Math.min(g[a][b], c);
        }
        int t = prim();
        if (t == INF) out.println("impossible");
        else out.println(t);
        out.close();
    }
}